package com.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.base.ResultSetHandler;
import com.dao.UserDAO;
import com.util.db.JdbcTemplate;
import com.vo.User;

public class UserDAOImpl implements UserDAO {
	private JdbcTemplate jdbcTemplate;

	public UserDAOImpl() {
		jdbcTemplate = new JdbcTemplate();
	}

	@Override
	public int save(User user) throws SQLException {
		return 0;
	}

	@Override
	public int delete(int id) throws SQLException {
		return 0;
	}

	@Override
	public int update(User user) throws SQLException {
		return 0;
	}

	@Override
	public User findById(int id) throws SQLException {
		return null;
	}

	@Override
	public List<User> find() throws SQLException {
		return null;
	}

	@Override
	public boolean find(String username, String password) throws SQLException {
		String sql = "SELECT * FROM user_login WHERE username=? AND password=?";

		User user = (User) jdbcTemplate.query(sql, new ResultSetHandler() {

			@Override
			public Object doHandler(ResultSet rs) throws SQLException {
				User user = null;

				if (rs.next()) {
					user = new User();
					user.setUsername(rs.getString("username"));
					user.setPassword(rs.getString("password"));
				}
				return user;
			}
		}, username, password);

		if (user != null) {
			return true;
		}
		return false;
	}

}
